package com.jiyongjun.component.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

import static com.jiyongjun.component.enums.LogConstant.LOG_EXCEPTION_KEYWORD;

/**
 * <h2></h2>
 * <h3>描述：</h3>
 *
 * @author yongjun.jyj
 * @since 2022/7/14 17:48
 */
@Slf4j
public class JsonUtils {
    public static final SerializeConfig SERIALIZE_CONFIG = new SerializeConfig();

    static {
        SERIALIZE_CONFIG.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss.SSS"));
    }

    public static String toJson(Object obj) {
        try {
            return JSON.toJSONString(obj, SERIALIZE_CONFIG);
        } catch (Exception e) {
            log.warn("{}|转换为json异常|msg={}", LOG_EXCEPTION_KEYWORD, e.getMessage(), e);
            return "";
        }
    }
}
